//
//  UserGraphInterface.cpp
//  p4
//
//  Created by CaoJun on 15/5/21.
//  Copyright (c) 2015年 com.baidao. All rights reserved.
//

#include "UserGraphInterface.h"
#include "YTXConfig.h"

using namespace ytx;
using namespace std;

UserGraph::UserGraph():lineWidth(1), lineColor(YTXConfig::getInstance()->style.userPaintDefaultColor), type(UserGraphType::UserGraphNone)
{

}

UserGraph::UserGraph (YTXJson::Value json)
{
    if (json.isMember("type")) {
        type = static_cast<UserGraphType::UserGraphType>(json["type"].asInt());
    }
    if (json.isMember("lineWidth")) {
        lineWidth = json["lineWidth"].asDouble();
    }

    if (json.isMember("lineColor")) {
        lineColor = json["lineColor"].asString();
    }

    if (json.isMember("p1")) {
        p1.dataX = YTXTime( Tools::parseLong( json["p1"]["dataX"].asString() ) );
        p1.preDataX = YTXTime( Tools::parseLong( json["p1"]["preDataX"].asString() ) );
        
        p1.dataY = json["p1"]["dataY"].asDouble();
        p1.dataIndexX = json["p1"]["dataIndexX"].asInt();
    }

    if (json.isMember("p2")) {
        p2.dataX = YTXTime( Tools::parseLong( json["p2"]["dataX"].asString() ) );
        p2.preDataX = YTXTime( Tools::parseLong( json["p2"]["preDataX"].asString() ) );
        
        p2.dataY = json["p2"]["dataY"].asDouble();
        p2.dataIndexX = json["p2"]["dataIndexX"].asInt();
    }

}

string UserGraph::toJSONString()
{
    YTXJson::FastWriter fastWriter;
    return fastWriter.write(toJSONValue());
}

YTXJson::Value UserGraph::toJSONValue()
{
    YTXJson::Value json;

    //什么类型做什么事

    json["type"] = (int)type;
    json["lineWidth"] = lineWidth;
    json["lineColor"] = lineColor;

    YTXJson::Value point1, point2;

    point1["dataX"] = ConvertToString( p1.dataX.toSeconds() );
    point1["preDataX"] = ConvertToString( p1.preDataX.toSeconds() );
    point1["dataY"] = p1.dataY;
    point1["dataIndexX"] = p1.dataIndexX;

    point2["dataX"] = ConvertToString( p2.dataX.toSeconds() );
    point2["preDataX"] = ConvertToString( p2.preDataX.toSeconds() );
    point2["dataY"] = p2.dataY;
    point2["dataIndexX"] = p2.dataIndexX;

    json["p1"] = point1;
    json["p2"] = point2;

    return json;
}

UserGraphInterface::UserGraphInterface(std::string userGraphId, LineType::LineType userGraphLineType):userGraphId(userGraphId), userGraphLineType(userGraphLineType)
{

}

string UserGraphInterface::UserGraphDefaultKey = "QuotationUserGraphUserDefault";

void UserGraphInterface::clearUserGraphByUserDefault()
{
    userGraphs.clear();
    NSString * groupName = YTXConfig::getInstance()->userDefaultGroupName == "" ? nil : [NSString stringWithUTF8String:YTXConfig::getInstance()->userDefaultGroupName.c_str()];
    [[[NSUserDefaults alloc] initWithSuiteName:groupName] setObject:@"" forKey: [NSString stringWithUTF8String:getUserDefaultKey().c_str()] ];
}

void UserGraphInterface::updateUserGraphToUserDefault(UserGraph userGraph)
{
    userGraphs.back() = userGraph;
}

void UserGraphInterface::saveUserGraphToUserDefault()
{
    YTXJson::Value userGraphsValue;

    YTXJson::FastWriter fastWriter;

    for (vector<UserGraph>::iterator it = userGraphs.begin(); it != userGraphs.end(); ++it) {
        userGraphsValue.append( (*it).toJSONValue() );
    }

    NSString * key = [NSString stringWithUTF8String:getUserDefaultKey().c_str()];
    
    NSString * groupName = YTXConfig::getInstance()->userDefaultGroupName == "" ? nil : [NSString stringWithUTF8String:YTXConfig::getInstance()->userDefaultGroupName.c_str()];
    [[[NSUserDefaults alloc] initWithSuiteName:groupName] setObject:[NSString stringWithUTF8String:fastWriter.write(userGraphsValue).c_str()] forKey: key];

}

void UserGraphInterface::cancelLastUserGraph()
{
    if (userGraphs.size() == 0) return;
    userGraphs.pop_back();
    this->saveUserGraphToUserDefault();
}

void UserGraphInterface::addUserGraphToUserDefault(UserGraph userGraph)
{
    userGraphs.push_back(userGraph);
}

void UserGraphInterface::loadUserGraphByUserDefault()
{
    vector<UserGraph> ret;
    
    NSString * groupName = YTXConfig::getInstance()->userDefaultGroupName == "" ? nil : [NSString stringWithUTF8String:YTXConfig::getInstance()->userDefaultGroupName.c_str()];
    NSString * jsonString = [[[NSUserDefaults alloc] initWithSuiteName:groupName] objectForKey:[NSString stringWithUTF8String:getUserDefaultKey().c_str()] ];
    

    if (!jsonString) {
        return;
    }

    YTXJson::Value jsonValue;
    YTXJson::Reader reader;
    bool parsingSuccessful = reader.parse([jsonString UTF8String], jsonValue);

    if (!parsingSuccessful) {
        return;
    }

    int i = 0, len = jsonValue.size();

    for (; i<len; i++)
    {
        ret.push_back(UserGraph(jsonValue[i]));
    }
    userGraphs = ret;
}

string UserGraphInterface::getUserDefaultKey()
{
    string key = UserGraphInterface::UserGraphDefaultKey+"+"+userGraphId+"+"+LineType::convertLineTypeToString( userGraphLineType );
    return key;
}

const vector<UserGraph> UserGraphInterface::getUserGraphs()
{
    return userGraphs;
}

void UserGraphInterface::setUserGraphs(vector<UserGraph> userGraph)
{
    this->userGraphs = userGraph;
}

UGCPoint UserGraphInterface::getUserGraphWithPoint(float x1, float y1)
{
    return UGCPoint();
}

